package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.*;
import com.itlong.cloud.POJO.PO.PropertyMonthCardPaymentRulePO;
import com.itlong.cloud.POJO.VO.property.PropertyMonthCardRuleGetOneVO;
import com.itlong.cloud.POJO.VO.property.PropertyMonthCardRuleGetPageVO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleDeleteDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleGetOneDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleUpdateDTO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyMonthCardRuleDao;
import com.itlong.cloud.property.service.IPropertyMonthCardRuleService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      智慧停车-月卡缴费规则管理服务层接口实现类
 * </desc>
 *
 * @createDate 2018/11/07
 */
@Service
public class PropertyMonthCardRuleServiceImpl implements IPropertyMonthCardRuleService {

    @Autowired
    private IPropertyMonthCardRuleDao iPropertyMonthCardRuleDao;

    /**
     * <desc>
     *      添加月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleSaveDTO 添加月卡缴费规则DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveMonthCardRule(PropertyMonthCardRuleSaveDTO propertyMonthCardRuleSaveDTO) throws Exception {
        Date nowTime = new Date();
        PropertyMonthCardPaymentRulePO propertyMonthCardPaymentRulePO = new PropertyMonthCardPaymentRulePO();
        propertyMonthCardPaymentRulePO.setMonthCardId(LogicIdUtil.bussinessId());
        propertyMonthCardPaymentRulePO.setRuleName(propertyMonthCardRuleSaveDTO.getRuleName());
        propertyMonthCardPaymentRulePO.setProjectId(propertyMonthCardRuleSaveDTO.getProjectId());
        propertyMonthCardPaymentRulePO.setMoney(new BigDecimal(propertyMonthCardRuleSaveDTO.getMoney()));
        propertyMonthCardPaymentRulePO.setUserId(propertyMonthCardRuleSaveDTO.getLoginUserId());
        propertyMonthCardPaymentRulePO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyMonthCardPaymentRulePO.setCreateTime(nowTime);
        propertyMonthCardPaymentRulePO.setUpdateTime(nowTime);
        Map<String, Object> map = SqlUtil.durableData(propertyMonthCardPaymentRulePO, PlatformConstants.TABLE_SAVE);
        return iPropertyMonthCardRuleDao.save(map);
    }

    /**
     * <desc>
     *      修改月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleUpdateDTO 修改月卡缴费规则DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateMonthCardRule(PropertyMonthCardRuleUpdateDTO propertyMonthCardRuleUpdateDTO) throws Exception {
        Date nowTime = new Date();
        PropertyMonthCardPaymentRulePO propertyMonthCardPaymentRulePO = new PropertyMonthCardPaymentRulePO();
        propertyMonthCardPaymentRulePO.setMonthCardId(propertyMonthCardRuleUpdateDTO.getMonthCardId());
        propertyMonthCardPaymentRulePO.setRuleName(propertyMonthCardRuleUpdateDTO.getRuleName());
        propertyMonthCardPaymentRulePO.setMoney(new BigDecimal(propertyMonthCardRuleUpdateDTO.getMoney()));
        propertyMonthCardPaymentRulePO.setUserId(propertyMonthCardRuleUpdateDTO.getLoginUserId());
        propertyMonthCardPaymentRulePO.setUpdateTime(nowTime);
        Map<String, Object> map = SqlUtil.durableData(propertyMonthCardPaymentRulePO, PlatformConstants.TABLE_UPDATE);
        return iPropertyMonthCardRuleDao.update(map);
    }

    /**
     * <desc>
     *      查询该月卡正使用的车辆数
     * </desc>
     *
     * @param monthCardId 月卡id
     * @return 车辆数
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer getCarCountByMonthCardId(String monthCardId) throws Exception {
        return iPropertyMonthCardRuleDao.getCarCountByMonthCardId(monthCardId);
    }

    /**
     * <desc>
     *      删除月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleDeleteDTO 删除月卡缴费规则DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deleteMonthCardRule(PropertyMonthCardRuleDeleteDTO propertyMonthCardRuleDeleteDTO) throws Exception {
        Date nowTime = new Date();
        PropertyMonthCardPaymentRulePO propertyMonthCardPaymentRulePO = new PropertyMonthCardPaymentRulePO();
        propertyMonthCardPaymentRulePO.setMonthCardId(propertyMonthCardRuleDeleteDTO.getMonthCardId());
        propertyMonthCardPaymentRulePO.setUserId(propertyMonthCardRuleDeleteDTO.getLoginUserId());
        propertyMonthCardPaymentRulePO.setStatus(DataStatusEnum.DELETE.getType());
        propertyMonthCardPaymentRulePO.setUpdateTime(nowTime);
        Map<String, Object> map = SqlUtil.durableData(propertyMonthCardPaymentRulePO, PlatformConstants.TABLE_UPDATE);
        return iPropertyMonthCardRuleDao.update(map);
    }

    /**
     * <desc>
     *      查询单条月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleGetOneDTO 查询单条月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyMonthCardRuleGetOneVO getOneMonthCardRule(PropertyMonthCardRuleGetOneDTO propertyMonthCardRuleGetOneDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyMonthCardRuleGetOneDTO);
        return iPropertyMonthCardRuleDao.getOneMonthCardRule(params);
    }

    /**
     * <desc>
     *      分页查询月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleGetPageDTO 查询单条月卡缴费规则DTO
     * @return 分页结果
     * @author wangzhi
     * @createDate 2018/11/08
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyMonthCardRuleGetPageVO> getMonthCardRulePage(PropertyMonthCardRuleGetPageDTO propertyMonthCardRuleGetPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyMonthCardRuleGetPageDTO);
        Page<PropertyMonthCardRuleGetPageVO> page = new Page<>();
        page.getPage().setPageSize(propertyMonthCardRuleGetPageDTO.getPageSize());
        page.getPage().setCurrentPage(propertyMonthCardRuleGetPageDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<PropertyMonthCardRuleGetPageVO> propertyMonthCardRuleGetPageVOS = iPropertyMonthCardRuleDao.getMonthCardRulePage(params);
        page.setRows(propertyMonthCardRuleGetPageVOS,iPropertyMonthCardRuleDao.getMonthCardRuleRecordCount(params));
        return page;
    }
}
